<?php
namespace Common\Lib;

class excel
{
	public $execl_url = '';
	//导出字段处理
	public $field_list = array(
			'catid'=>'分类',
			'pname'=>'商品名称',
	);
	//excel表header
	public $excel_header = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ','BA','BB','BC','BD','BE','BF','BG','BH','BI','BJ','BK','BL','BM','BN');
	//导入字段设置
	public $column_list = array('card_number','price','desc');


	function __construct($excel_url  = ''){
		$this->execl_url = $excel_url;
        vendor('PHPExcel.PHPExcel','simplewind/Core/Library/Vendor/','.php');
        vendor('PHPExcel.IOFactory','simplewind/Core/Library/Vendor/PHPExcel/','.php');
        vendor('PHPExcel.Cell','simplewind/Core/Library/Vendor/PHPExcel/','.php');
	}

	//导入  获取excel表数据
	public function get_excel_data()
	{

		$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
		$objReader->setReadDataOnly(true);
		$objPHPExcel = $objReader->load($this->execl_url);
		$objWorksheet = $objPHPExcel->getActiveSheet();
		$highestRow = $objWorksheet->getHighestRow();
		$highestColumn = $objWorksheet->getHighestColumn();
		$highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn);

		$excelData = array();
		for ($row = 2; $row <= $highestRow; $row++) {
			$column_data = array();
			for($col = 0;$col < $highestColumnIndex;$col++){
				//获取excel表数据
				if($col == 0){	//第一列为空 不导入
					$data =(string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
					if(empty($data)){
						break;
					}
				}else{
					$data =(string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
				}
				//给数据加上key值

				$column_name = $this->column_list[$col];
				if(is_array($column_name)){
					foreach($column_name AS $citem){
						$column_data[$citem] = $data;
					}
				}elseif(!empty($column_name)){
					$column_data[$column_name] = $data;
				}
				//数据处理
				$column_data = $this->excel_data_handle($column_data,$row);

				//而外的固定数据
				$other_data = $this->other_data();
				$column_data = array_merge($other_data,$column_data);

			}
			if(!empty($column_data)){
				$excelData[] = $column_data;
			}
		}
		return $excelData;
	}

	/**
	 * excel中的数据处理
	 */
	public function excel_data_handle($column_data,$row){
//		foreach($column_data AS $ckey => &$cdata){
//			if($ckey == 'cover'){	//封面图处理
//				$cdata = "http://img.matrostyle.com/uploadfile/wxhd/kanjia/small/1/".($row-1).".jpg";
//			}elseif($ckey == 'pimages'){	//商品大图处理
//				$cdata = serialize(array("http://img.matrostyle.com/uploadfile/wxhd/kanjia/big/1/".($row-1).".jpg"));
//			}elseif($ckey == 'starttime'){
//				$cdata = strtotime("2017-06-15 00:00:00");
//			}elseif($ckey == 'endtime'){
//				$cdata = strtotime("2017-06-18 00:00:00");
//			}
//		}
		return $column_data;
	}

	/**
	 * 而外的固定数据添加
	 * */
	function other_data(){
//		$data = array(
//			'helpmode'=>0,
//			'ordermode'=>1,
//			'follow'=>1,
//			'status'=>1,
//			'sharetitle'=>'一起来帮我砍价吧',
//			'shareimage'=>'http://img.matrostyle.com/uploadfile/wxhd/kanjia/img/share-ico.jpg',
//			'sharedesc'=>'分享好友，助力砍价'
//		);
        $data = array();
		return $data;
	}

	/**
	 * 导出
	 */
	public function export_data($title,$export_title,$data){


		$phpexcel = new \PHPExcel();
		$fileName = $title;
		$phpexcel->getProperties()->setTitle($title);
		$phpexcel->setActiveSheetIndex(0);
		$phpexcel->getActiveSheet()->setTitle($title);

		$phpexcel->getDefaultStyle()->getFont()->setName("微软雅黑");

        $export_num = 0;
		foreach($export_title as $index => $item){
			$phpexcel->getActiveSheet()->setCellValue($this->excel_header[$export_num]."1", $item);
            $export_num++;
		}

		$ki = 2;
		foreach ($data as $key => $value) {
            $export_num = 0;
			foreach($export_title as $index2 => $item2){
				$data_value = $value[$index2];
				$data_value = $this->get_field_value($index2,$data_value);
				$phpexcel->getActiveSheet()->getStyle ($this->excel_header[$export_num])->getNumberFormat()->setFormatCode ("@");
				$phpexcel->getActiveSheet()->setCellValue($this->excel_header[$export_num].$ki," ".$data_value);

                $export_num++;
			}
			$ki ++;
		}
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
		header("Content-Type:application/force-download");
		header("Content-Type:application/vnd.ms-execl");
		header("Content-Type:application/octet-stream");
		header("Content-Type:application/download");
		header('Content-Disposition:attachment;filename="'.$fileName.'.xls"');
		header("Content-Transfer-Encoding:binary");
		$phpexcel->createSheet();
		$objWriter = \PHPExcel_IOFactory::createWriter($phpexcel, 'Excel5');
		$objWriter->save('php://output');
	}

	/**
	 * 获取字段值
	 */
	function get_field_value($field,$data_value){
		switch($field){
			case 'create_time':
                    if(strstr($data_value,'-')){
                        return $data_value;
                    }else{
                        $data_value = date('Y-m-d H:i:s',$data_value);
                    }

				break;
		}
		return $data_value;
	}


}
?>